﻿using KalevaAalto.DataPart.Models;
using Org.BouncyCastle.Asn1.X509.Qualified;
using SQLitePCL;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KalevaAalto.DataPart.Models
{



    public class DataColumnMatch
    {
        private ICollection<string> _sourceColumnNames;
        private string _targetColumnName;
        private Type _type;

        public DataColumnMatch(IEnumerable<string> sourceColumnNames, string targetColumnName,Type type) 
        {
            _sourceColumnNames = sourceColumnNames.ToHashSet();
            if (_sourceColumnNames.Count() == 0) _sourceColumnNames.Add(targetColumnName);
            _targetColumnName = targetColumnName;
            _type = type;
        }

        public ICollection<string> SourceColumnNames => _sourceColumnNames;
        public string ColumnName =>_targetColumnName; 
        public Type Type =>_type; 




    }


    public class DataColumnMatchCollection : ICollection<DataColumnMatch>
    {
        private ICollection<DataColumnMatch> _list = new List<DataColumnMatch>();

        public int Count => _list.Count;

        public bool IsReadOnly => false;

        public void Add(DataColumnMatch item)=> _list.Add(item);


        public void Add(IEnumerable<string> sourceColumnNames, string targetColumnName) 
            => Add(new DataColumnMatch(sourceColumnNames,targetColumnName,typeof(object)));

        public void Add(string[] sourceColumnNames, string targetColumnName,Type type)
            => Add(new DataColumnMatch(sourceColumnNames, targetColumnName, type));



        public void Clear()=> _list.Clear();


        public bool Contains(DataColumnMatch item)=> _list.Contains(item);


        public void CopyTo(DataColumnMatch[] array, int arrayIndex)=> _list.CopyTo(array, arrayIndex);


        public IEnumerator<DataColumnMatch> GetEnumerator()=> _list.GetEnumerator();


        public bool Remove(DataColumnMatch item)=> _list.Remove(item);


        IEnumerator IEnumerable.GetEnumerator()=> GetEnumerator();





        public DataTable GenerateNewDataTable(string tableName = EmptyString)
        {
            DataTable dt = new DataTable(tableName);

            foreach(var dataColumnMatch in this)
            {
                dt.Columns.Add(dataColumnMatch.ColumnName, dataColumnMatch.Type);
            }

            return dt;
        }

    }
}



